import 'package:ceramics/model/ab_like.dart';
import 'package:ceramics/model/ab_singleton.dart';
import 'package:ceramics/pages/main/home/ab_home_page.dart';
import 'package:ceramics/styles/ab_styles.dart';
import 'package:ceramics/util/ab_api.dart';
import 'package:ceramics/util/ab_log_util.dart';
import 'package:ceramics/widgets/ab_elevate_button.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'hot/ab_hot_page.dart';
import 'mine/ab_mine_page.dart';
import 'publish/ab_publish_page.dart';
import 'video/ab_video_home_page.dart';

class ABMainTabController extends GetxController {
  final RxInt tabIndex = 0.obs;

  @override
  void onInit() {
    super.onInit();
    ABLike.queryAllLikes();
    ABSingleton.fetchConfig();
  }

}

class ABMainTabPage extends GetView<ABMainTabController> {
  const ABMainTabPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Obx(() {
      return Scaffold(
        resizeToAvoidBottomInset: false,
        floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
        floatingActionButton: IconButton(icon: Image.asset("images/ceramics_74.webp"), onPressed: () {
          Get.find<ABVideoHomePageController>().videoPlayerController?.pause();
          Get.to(()=>const ABPublishPage());
        },iconSize: 60),
        body: IndexedStack(
          index: controller.tabIndex.value,
          children: const [
            ABHomePage(),
            ABVideoHomePage(),
            SizedBox(),
            ABHotPage(),
            ABMinePage(),
          ],
        ),
        extendBody: true,
        bottomNavigationBar: Container(
          decoration: const BoxDecoration(
            image: DecorationImage(
              image: AssetImage("images/tabbar_bg.webp"),
              fit: BoxFit.cover,
            ),
          ),
          child: Theme(
            data: ThemeData(
              splashColor: Colors.transparent,
              highlightColor: Colors.transparent,
            ),
            child: BottomNavigationBar(
              backgroundColor: Colors.transparent,
              type: BottomNavigationBarType.fixed,
              elevation: 0,
              currentIndex: controller.tabIndex.value,
              onTap: (idx) {
                LogPvManger.stat(page: 'tab_$idx');
                if (idx == 2) {
                  Get.find<ABVideoHomePageController>().videoPlayerController?.pause();
                  Get.to(()=>const ABPublishPage(),transition: Transition.downToUp);
                  return;
                }
                if (idx == 1) {
                  // 播放视频
                  Get.find<ABVideoHomePageController>().videoPlayerController?.play();
                } else {
                  Get.find<ABVideoHomePageController>().videoPlayerController?.pause();
                }
                controller.tabIndex.value = idx;
              },
              items: [
                _buildTabBarItem(
                    "images/ceramics_59.webp", "images/ceramics_3.webp"),
                _buildTabBarItem(
                    "images/ceramics_83.webp", "images/ceramics_21.webp"),
                const BottomNavigationBarItem(icon: SizedBox(), label: ''),
                _buildTabBarItem(
                    "images/ceramics_48.webp", "images/ceramics_19.webp"),
                _buildTabBarItem(
                    "images/ceramics_50.webp", "images/ceramics_5.webp"),
              ],
            ),
          ),
        ),
      );
    });
  }

  BottomNavigationBarItem _buildTabBarItem(String image, String activeImage) {
    double size = 30.0;
    return BottomNavigationBarItem(
        icon: Image.asset(image, width: size),
        activeIcon: Image.asset(activeImage, width: size),
        label: '');
  }
}
